#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>

void gtTripStart(gtranDevice* gtrandevice);

void gtOverStart1(gtranDevice* gtrandevice);
void gtOverStart2(gtranDevice* gtrandevice);

void gtZeroStart1(gtranDevice* gtrandevice);
void gtZeroStart2(gtranDevice* gtrandevice);

void gtLowStart(gtranDevice* gtrandevice);

void gtTripStart(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[0];

    Ia = gtrandevice->halfphasor[3];
    Ib = gtrandevice->halfphasor[4];
    Ic = gtrandevice->halfphasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->quickCurStartFlag = 1;
        gtrandevice->startTime[0] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "速断过流保护启动");
    }

}

void gtOverStart1(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[1];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->overCurStartFlag1 = 1;
        gtrandevice->startTime[1] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "过流保护I段启动");
    }

}

void gtOverStart2(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[2];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->overCurStartFlag2 = 1;
        gtrandevice->startTime[2] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "过流保护II段启动");

    }

}


void gtZeroStart1(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[3];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->zeroCurStartFlag1 = 1;
        gtrandevice->startTime[3] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "高压侧接地保护I段启动");
    }

}

void gtZeroStart2(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[4];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->zeroCurStartFlag2 = 1;
        gtrandevice->startTime[4] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "高压侧接地保护II段启动");
    }

}

void gtLowStart(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time;
    time = gtrandevice->time;
    Iset = gtrandevice->gtranSetValue[5];

    Ia = gtrandevice->phasor[6];
    Ib = gtrandevice->phasor[7];
    Ic = gtrandevice->phasor[8];

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > (0.95 * Iset)) && (time > 0.1)){
        gtrandevice->lowCurStartFlag = 1;
        gtrandevice->startTime[5] = gtrandevice->time;
        gtranWriteLog(gtrandevice, "低压侧零序过流保护启动");
    }

}